package com.wn.framework.base

import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import com.wn.framework.R
import com.wn.framework.ext.viewDataBind
import com.wn.framework.ext.viewLifeCycleOwner
import com.wn.framework.utils.LoadingUtils

/**
 * @Author: wn
 * @Date: 2024/8/20 16:38
 * @Description: 抽象的公用 BaseActivity
 */
abstract class BaseActivity<T : ViewDataBinding> : AppCompatActivity {
  constructor() : super()

  // 次构造函数 传递 LayoutRes  取代 abstract获取 view
  constructor(@LayoutRes layout: Int) : super(layout)

  protected val tag: String? = this::class.java.simpleName

  protected val mBinding: T by lazy {
    viewDataBind(getLayoutRes())
  }

  // 弹窗 工具类
  private val dialogUtils: LoadingUtils by lazy {
    LoadingUtils(this)
  }

  @LayoutRes
  protected abstract fun getLayoutRes(): Int

  /**
   * 初始化视图
   */
  protected open fun initView() {}

  /**
   * 初始化数据
   */
  protected open fun initData() {}

  /**
   * 初始化配置
   */
  protected open fun initConfig() {}

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    mBinding.lifecycleOwner = viewLifeCycleOwner
    initConfig()
    initData()
    initView()
  }

  override fun onDestroy() {
    super.onDestroy()
    mBinding.unbind()
  }

  /**
   * 加载中……弹框   不传值 默认加载中
   * @param str String
   */
  protected fun showLoading(str: String = getString(R.string.default_loading)) {
    dialogUtils.showLoading(str)
  }

  /**
   * 关闭提示框
   */
  protected fun dismissLoading() {
    dialogUtils.dismissLoading()
  }
}